/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_SSAP_CONFIG_H
#define SLE_SSAP_CONFIG_H
#include <stdint.h>
#include <stdbool.h>

#define UUID_LEN 16

typedef struct {
    uint8_t len;                /*!< @if Eng uuid len
                                     @else   UUID 长度 @endif */
    uint8_t uuid[UUID_LEN]; /*!< @if Eng uuid
                                     @else   UUID字段 @endif */
} SleUuid;

typedef enum {
    SLE_SSAP_PROPERTY_TYPE                        = 0x00, /*!< @if Eng property value
                                                      @else   特征值 @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION     = 0x01, /*!< @if Eng user description descriptor
                                                      @else   属性说明描述符 @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_CLIENT_CONFIGURATION = 0x02, /*!< @if Eng client configuration descriptor
                                                      @else   客户端配置描述符 @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_SERVER_CONFIGURATION = 0x03, /*!< @if Eng server configuration descriptor
                                                      @else   服务端配置描述符 @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_PRESENTATION_FORMAT  = 0x04, /*!< @if Eng presentation format descriptor
                                                      @else   格式描述符 @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_RFU                  = 0x05, /*!< @if Eng RFU descriptor, 0x05 – 0x1F
                                                      @else   服务管理保留描述符，0x05 – 0x1F @endif */
    SLE_SSAP_DESCRIPTOR_TYPE_CUSTOM               = 0xFF, /*!< @if Eng custom descriptor
                                                      @else   厂商自定义描述符 @endif */
} SleSsapPropertyType_t;
typedef uint8_t SleSsapPropertyType;

#ifndef SleSsapPropertyTypeStr
#define SleSsapPropertyTypeStr(type)\
        (type == SLE_SSAP_PROPERTY_TYPE) ? "property" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION) ? "user description" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_CLIENT_CONFIGURATION) ? "client configuration" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_SERVER_CONFIGURATION) ? "server configuration" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_PRESENTATION_FORMAT) ? "presentation format" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_RFU) ? "RFU" :\
        (type == SLE_SSAP_DESCRIPTOR_TYPE_CUSTOM) ? "custom reserved" : "Unknown"
#endif

/**
 * @if Eng
 * @brief  Enum of write types for ssap clients to write property and descriptor.
 * @else
 * @brief  定义SSAP client写property和descriptor时的写类型。
 * @endif
 */
typedef enum {
    SSAP_WRITE_NO_RSPONSE           = 0x01, /*!< @if Eng type of wirte with no server response
                                             @else 只写，且不需要server回复响应 @endif */
    SSAP_WRITE_DEFAULT_WITH_RSPONSE = 0x02, /*!< @if Eng type of wirte with server response
                                             @else 只写，且需要server回复响应 @endif */
} SsapWriteType_t;
typedef uint8_t SsapWriteType;

/**
 * @if Eng
 * @brief  SSAP attribute permission.
 * @else
 * @brief  SSAP attribute操作权限。
 * @endif
 */
typedef enum {
    SSAP_PERMISSION_CAN_READ                = 0x01, /*!< @if Eng readable
                                                     @else   可读 @endif */
    SSAP_PERMISSION_CAN_WRITE               = 0x02, /*!< @if Eng writable
                                                     @else   可写 @endif */
    SSAP_PERMISSION_NEED_ENCRYPTION     = 0x04, /*!< @if Eng need encryption
                                                     @else   需要加密 @endif */
    SSAP_PERMISSION_NEED_AUTHENTICATION = 0x08, /*!< @if Eng need authentication
                                                     @else   需要认证 @endif */
    SSAP_PERMISSION_NEED_AUTHORIZATION  = 0x10, /*!< @if Eng need authorization
                                                     @else   需要授权 @endif */
} SsapPermissionType_t;
typedef uint16_t SsapPermissionType;

/**
 * @if Eng
 * @brief  SSAP find type.
 * @else
 * @brief  SSAP 查找类型
 * @endif
 */
typedef enum {
    SSAP_FIND_TYPE_SERVICE_STRUCTURE_TYPE = 0x00, /*!< @if Eng service structure
                                                  @else   服务结构 @endif */
    SSAP_FIND_TYPE_PRIMARY_SERVICE_TYPE   = 0x01, /*!< @if Eng primary service
                                                  @else   首要服务 @endif */
    SSAP_FIND_TYPE_REFERENCE_SERVICE_TYPE = 0x02, /*!< @if Eng reference service
                                                  @else   引用服务 @endif */
    SSAP_FIND_TYPE_PROPERTY_TYPE          = 0x03, /*!< @if Eng property
                                                  @else   属性 @endif */
    SSAP_FIND_TYPE_METHOD_TYPE            = 0x04, /*!< @if Eng method
                                                  @else   方法 @endif */
    SSAP_FIND_TYPE_EVENT_TYPE             = 0x05, /*!< @if Eng event
                                                  @else   事件 @endif */
} SsapFindType_t;
typedef uint8_t SsapFindType;

#ifndef SsapFindTypeStr
#define SsapFindTypeStr(x) \
        (x == SSAP_FIND_TYPE_SERVICE_STRUCTURE_TYPE) ? "service Structure" :     \
        (x == SSAP_FIND_TYPE_PRIMARY_SERVICE_TYPE) ? "primary Service" :         \
        (x == SSAP_FIND_TYPE_REFERENCE_SERVICE_TYPE) ? "referen ceService" :     \
        (x == SSAP_FIND_TYPE_PROPERTY_TYPE) ? "property" :                       \
        (x == SSAP_FIND_TYPE_METHOD_TYPE) ? "method" :                           \
        (x == SSAP_FIND_TYPE_EVENT_TYPE) ? "event" : "Unknown"
#endif

/**
 * @if Eng
 * @brief  SSAP property type.
 * @else
 * @brief  SSAP 特征类型。
 * @endif
 */
typedef enum {
    SSAP_PROPERTY_TYPE_VALUE_TYPE                  = 0x00, /*!< @if Eng property value
                                                      @else   特征值 @endif */
    SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION_TYPE     = 0x01, /*!< @if Eng user description descriptor
                                                      @else   属性说明描述符 @endif */
    SSAP_DESCRIPTOR_TYPE_CLIENT_CONFIGURATION_TYPE = 0x02, /*!< @if Eng client configuration descriptor
                                                      @else   客户端配置描述符 @endif */
    SSAP_DESCRIPTOR_TYPE_SERVER_CONFIGURATION_TYPE = 0x03, /*!< @if Eng server configuration descriptor
                                                      @else   服务端配置描述符 @endif */
    SSAP_DESCRIPTOR_TYPE_PRESENTATION_FORMAT_TYPE  = 0x04, /*!< @if Eng presentation format descriptor
                                                      @else   格式描述符 @endif */
    SSAP_DESCRIPTOR_TYPE_RFU_TYPE                  = 0x05, /*!< @if Eng RFU descriptor, 0x05 – 0x1F
                                                      @else   服务管理保留描述符，0x05 – 0x1F @endif */
    SSAP_DESCRIPTOR_TYPE_CUSTOM_TYPE               = 0xFF, /*!< @if Eng custom descriptor
                                                      @else   厂商自定义描述符 @endif */
} SsapPropertyType_t;
typedef uint8_t SsapPropertyType;

#ifndef SsapPropertyTypeStr
#define SsapPropertyTypeStr(type)                                       \
        (type == SSAP_PROPERTY_TYPE_VALUE_TYPE) ? "Value" :                       \
        (type == SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION_TYPE) ? "UserDescription" : \
        (type == SSAP_DESCRIPTOR_TYPE_CLIENT_CONFIGURATION_TYPE) ? "ClientConfiguration" : \
        (type == SSAP_DESCRIPTOR_TYPE_SERVER_CONFIGURATION_TYPE) ? "ServerConfiguration" : \
        (type == SSAP_DESCRIPTOR_TYPE_PRESENTATION_FORMAT_TYPE) ? "PresentationFormat" : \
        (type == SSAP_DESCRIPTOR_TYPE_RFU_TYPE) ? "RFU" :                                       \
        (type == SSAP_DESCRIPTOR_TYPE_CUSTOM_TYPE) ? "Custom" : "Unknown"
#endif

/**
 * @if Eng
 * @brief  SSAP operation indication.
 * @else
 * @brief  SSAP 操作指示。
 * @endif
 */
typedef enum {
    SSAP_OPERATEIND_BIT_READ = 0x01,              /*!< @if Eng data values can be read
                                                               @else   数据值可被读取 @endif */
    SSAP_OPERATEIND_BIT_WRITE_NO_RSP = 0x02,      /*!< @if Eng data values can be written without feedback.
                                                               @else   数据值可被写入，写入后无反馈 @endif */
    SSAP_OPERATEIND_BIT_WRITE = 0x04,             /*!< @if Eng data values can be written. After being written,
                                                                       a feedback is generated to the client.
                                                               @else   数据值可被写入，写入后产生反馈给客户端 @endif */
    SSAP_OPERATEIND_BIT_NOTIFY = 0x08,            /*!< @if Eng data value send to client by notification.
                                                               @else   数据值通过通知方式传递给客户端 @endif */
    SSAP_OPERATEIND_BIT_INDICATE = 0x10,          /*!< @if Eng data value send to client by indication.
                                                               @else   数据值通过指示方式传递给客户端 @endif */
    SSAP_OPERATEIND_BIT_BROADCAST = 0x20,         /*!< @if Eng data value can be carried in broadcast
                                                               @else   数据值可携带在广播中 @endif */
    SSAP_OPERATEIND_BIT_DESCRITOR_WRITE = 0x100,  /*!< @if Eng data value description descriptor can be
                                                                       written
                                                               @else   数据值说明描述符可被写入 @endif */
    SSAP_OPERATEIND_MAX,                          /*!< @if Eng maximum of operation indication
                                                               @else   操作指示最大值 @endif */
} SsapOperateIndication_t;
typedef uint32_t SsapOperateIndication;

/**
 * @if Eng
 * @brief  Struct of ssap info exchange
 * @else
 * @brief  ssap 信息交换结构体。
 * @endif
 */
typedef struct {
    uint32_t mtuSize; /*!< @if Eng mtu size
        ·                   @else   mtu大小 @endif */
    uint16_t version;  /*!< @if Eng version
                            @else   版本 @endif */
} SsapMtuInfo;

// TODO:当前版本SDK未给出此定义
typedef enum {
    SSAP_PROPERTY_DESCRIPTOR_TYPE_UNKNOWN = 0x00,
} SsapPropertyDescriptorType_t;
typedef uint8_t SsapPropertyDescriptorType;

#define ERROR_CODE_SLE_SSAP_BASE   0x80006100
/**
 * @if Eng
 * @brief  SLE SSAP protocol error code.
 * @else
 * @brief  SLE SSAP 协议错误码。
 * @endif
 */
typedef enum {
    ERROR_CODE_SSAP_INVALID_PDU = ERROR_CODE_SLE_SSAP_BASE + 0x01,                      /*!< @if Eng invalid PDU
                                                                                       @else   服务端接收的 PDU 无效
                                                                                       @endif */
    ERROR_CODE_SSAP_PDU_NOT_SUPPORT = ERROR_CODE_SLE_SSAP_BASE + 0x02,                  /*!< @if Eng PDU not support
                                                                                       @else   服务端不支持处理接收的
                                                                                               PDU
                                                                                       @endif */
    ERROR_CODE_SSAP_UNKNOW = ERROR_CODE_SLE_SSAP_BASE + 0x03,                           /*!< @if Eng PDU unknow
                                                                                       @else   服务端执行请求时发生
                                                                                               未知错误
                                                                                       @endif */
    ERROR_CODE_SSAP_INVALID_HANDLE = ERROR_CODE_SLE_SSAP_BASE + 0x04,                   /*!< @if Eng invalid handle
                                                                                       @else   请求中的句柄无效
                                                                                       @endif */
    ERROR_CODE_SSAP_INSUFFICIENT_RESOURCES = ERROR_CODE_SLE_SSAP_BASE + 0x05,           /*!< @if Eng insufficient resources
                                                                                       @else   服务端没有足够资源完成
                                                                                               请求
                                                                                       @endif */
    ERROR_CODE_SSAP_PROHIBIT_READING = ERROR_CODE_SLE_SSAP_BASE + 0x06,                 /*!< @if Eng prohibit reading
                                                                                       @else   服务端禁止客户端读取值
                                                                                       @endif */
    ERROR_CODE_SSAP_PROHIBIT_WRITE = ERROR_CODE_SLE_SSAP_BASE + 0x07,                   /*!< @if Eng prohibit writing
                                                                                       @else   服务端禁止客户端写入值
                                                                                       @endif */
    ERROR_CODE_SSAP_CLIENT_NOT_AUTHENTICATED = ERROR_CODE_SLE_SSAP_BASE + 0x08,         /*!< @if Eng client no authentication
                                                                                       @else   客户端未经过认证
                                                                                       @endif */
    ERROR_CODE_SSAP_CLIENT_NOT_AUTHORIZATION = ERROR_CODE_SLE_SSAP_BASE + 0x09,         /*!< @if Eng client no authorizaion
                                                                                       @else   客户端未被授权
                                                                                       @endif */
    ERROR_CODE_SSAP_BEARER_NOT_ENCRYPTED = ERROR_CODE_SLE_SSAP_BASE + 0x0A,             /*!< @if Eng bearer not encrypted
                                                                                       @else   传输 PDU 的承载未加密
                                                                                       @endif */
    ERROR_CODE_SSAP_ENTRIES_NOT_FOUND = ERROR_CODE_SLE_SSAP_BASE + 0x0B,                /*!< @if Eng entries not found
                                                                                       @else   服务端未找到对应条目
                                                                                       @endif */
    ERROR_CODE_SSAP_DATA_NOT_FOUND = ERROR_CODE_SLE_SSAP_BASE + 0x0C,                   /*!< @if Eng data not found
                                                                                       @else   服务端未找到对应类型数据
                                                                                       @endif */
    ERROR_CODE_SSAP_INCORRECT_DATA_TYPE = ERROR_CODE_SLE_SSAP_BASE + 0x0D,              /*!< @if Eng data type incorrected
                                                                                       @else   客户端发送写入数据类型
                                                                                               不符的错误
                                                                                       @endif */
    ERROR_CODE_SSAP_INCORRECT_DATA_VALUE = ERROR_CODE_SLE_SSAP_BASE + 0x0E,             /*!< @if Eng data value incorrected
                                                                                       @else   客户端发送写入值不符
                                                                                               的错误
                                                                                       @endif */
    ERROR_CODE_SSAP_VALUE_OUT_OF_RANGE = ERROR_CODE_SLE_SSAP_BASE + 0x0F,               /*!< @if Eng data value incorrected
                                                                                       @else   客户端写入的值超出范围
                                                                                       @endif */
    ERROR_CODE_SSAP_UPPERLAYER_APPLICATION_ERROR_MIN = ERROR_CODE_SLE_SSAP_BASE + 0xAF, /*!< @if Eng reserve
                                                                                       @else   预留给上层协议定义应用
                                                                                               错误
                                                                                       @endif */
    ERROR_CODE_SSAP_UPPERLAYER_APPLICATION_ERROR_MAX = ERROR_CODE_SLE_SSAP_BASE + 0xFF, /*!< @if Eng reserve
                                                                                       @else   预留给上层协议定义应用
                                                                                               错误
                                                                                       @endif */
} SleSsapErrorCode_t;
typedef uint32_t SleSsapErrorCode;
#endif //SLE_SSAP_CONFIG_H